package com.galeno.topn;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Author:   galeno
 * Date:     21/07/25
 * Description:
 * 如果想封装json数据
 * Bean中的属性要和json串中的属性保持一致
 * {"movie":"2355","rate":"5","timeStamp":"978824291","uid":"1"}
 *
 * 自定义类实现HDP序列化机制
 *   implements Writable
 *  序列化的本质就是将内存对象转二进制    写到磁盘  网络传输
 *  反序列化的本质 就是将二进制变成内存对象  从磁盘读取  接受网络数据
 *
 */
public class MovieBean implements WritableComparable<MovieBean> {
     // 电影id
    private  int movie ;
    private double rate ;
    private  String  timeStamp ;
    private  int uid ;

    public int getMovie() {
        return movie;
    }

    public void setMovie(int movie) {
        this.movie = movie;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @Override
    public String toString() {
        return "MovieBean{" +
                "movie=" + movie +
                ", rate=" + rate +
                ", timeStample='" + timeStamp + '\'' +
                ", uid=" + uid +
                '}';
    }

    /**
     * 序列化  写
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(movie);
        dataOutput.writeDouble(rate);
        dataOutput.writeUTF(timeStamp);
        dataOutput.writeInt(uid);
    }
     // 反序列化  读
    // 注意写出属性的顺序  属性数据类型  属性的个数
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        movie = dataInput.readInt();
        rate = dataInput.readDouble();
        timeStamp = dataInput.readUTF() ;
        uid = dataInput.readInt() ;
    }

    /**
     * 先按电影id
     *    分数降序
     * @param o
     * @return
     */
    @Override
    public int compareTo(MovieBean o) {
        return  Integer.compare(this.movie , o.movie)==0?Double.compare(o.getRate(),this.rate):Integer.compare(o.movie,this.movie) ;
    }
}
